package com.ruifu.proj.controller;

import com.fhzncloud.cloud.common.core.util.R;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.proj.dto.ptmain.PmFreePtMainInsertDTO;
import com.ruifu.proj.service.PmPtMainService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/freePtMain")
public class PmFreePtMainController {


    @Autowired
    private PmPtMainService pmPtMainService;


    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }


    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;


    /**
     * 新建游离态项目
     *
     * @param pmFreePtMainInsertDTO
     * @return
     */
    @PostMapping(value = "/create")
    public R create(@RequestBody PmFreePtMainInsertDTO pmFreePtMainInsertDTO) {
        if (StringUtils.isBlank(pmFreePtMainInsertDTO.getPtName())) {
            return R.failed("项目名缺失！");
        }
        try {
            pmPtMainService.create(pmFreePtMainInsertDTO, cloudUser);
           return R.ok("新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }


}
